package vlms.dao;

import java.sql.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;

import com.sun.org.apache.bcel.internal.generic.NEW;

import vlms.bean.Person;
import vlms.exception.PersonException;

public class PersonDAO {

	private static final int INSERT_OPERATION = 1;
	private static final int DELETE_OPERATION = 2;
	private static final int UPDATE_OPERATION = 3;
	static HashMap<Integer, Person> primaryList = new HashMap<Integer, Person>();
	static HashMap<String, Person> emailIdList = new HashMap<String, Person>();
	static HashMap<String, LinkedList<Person>> nameList = new HashMap<String, LinkedList<Person>>();
	static HashMap<String, LinkedList<Person>> lNameList = new HashMap<String, LinkedList<Person>>();
	static HashMap<String, LinkedList<Person>> zipCodeList = new HashMap<String, LinkedList<Person>>();
	static HashMap<String, LinkedList<Person>> cityList = new HashMap<String, LinkedList<Person>>();
	static HashMap<String, LinkedList<Person>> stateList = new HashMap<String, LinkedList<Person>>();
	
	private static Set<String> abbreviations = new HashSet<String>( 
	           ( Arrays.asList( new String[] {
			"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", 
	               "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", 
			"MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", 
	               "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"
			} ) ) );

	//Static Method Starts
	private static void staticPopulateVO(Person dto, ResultSet rs) throws SQLException {
		if (rs == null || dto == null)
		{
			return;
		}
		dto.setPersonid(rs.getInt("personId"));
		dto.setEmailid(rs.getString("emailId"));
		dto.setFirstname(rs.getString("firstName"));
		dto.setLastname(rs.getString("lastName"));
		dto.setAddress(rs.getString("address"));
		dto.setCity(rs.getString("city"));
		dto.setState(rs.getString("state"));
		dto.setZipcode(rs.getString("zipCode"));
		dto.setMembertype(rs.getString("memberType"));
		dto.setCreditcardtype(rs.getString("creditCardType"));
		dto.setCreditcardnum(rs.getString("creditCardNum"));
		dto.setCreditcardcvv(rs.getInt("creditCardCVV"));
	}
	
	private static void initializeList(Connection con)
	{		
		try {
			PreparedStatement stmt = null;
			ResultSet rs = null;

			String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person order by personId";
			stmt = con.prepareStatement(SQL_STATEMENT);
			rs = stmt.executeQuery();
			if (rs == null)
				return;
			while (rs.next()) {
				Person dto = new Person();
				staticPopulateVO(dto, rs);
				if (dto.getMembertype().equals("AD"))
				{
					continue;
				}
				System.out.println("Person id : " + dto.getPersonid());
				primaryList.put(new Integer(dto.getPersonid()), dto);
				emailIdList.put(dto.getEmailid(), dto);

				if (nameList.get(dto.getFirstname()) == null) {
					LinkedList<Person> lPerson = new LinkedList<Person>();
					nameList.put(dto.getFirstname(), lPerson);
				}
				nameList.get(dto.getFirstname()).add(dto);

				if (lNameList.get(dto.getLastname()) == null) {
					LinkedList<Person> lPerson = new LinkedList<Person>();
					lNameList.put(dto.getLastname(), lPerson);
				}
				lNameList.get(dto.getLastname()).add(dto);
				
				if (cityList.get(dto.getCity()) == null) {
					LinkedList<Person> lPerson = new LinkedList<Person>();
					cityList.put(dto.getCity(), lPerson);
				}
				cityList.get(dto.getCity()).add(dto);
				
				if (stateList.get(dto.getState()) == null) {
					LinkedList<Person> lPerson = new LinkedList<Person>();
					stateList.put(dto.getState(), lPerson);
				}
				stateList.get(dto.getState()).add(dto);
				
				if (zipCodeList.get(dto.getZipcode()) == null) {
					LinkedList<Person> lPerson = new LinkedList<Person>();
					zipCodeList.put(dto.getZipcode(), lPerson);
				}
				zipCodeList.get(dto.getZipcode()).add(dto);
			}
			
		} catch (SQLException se) {

			// The exception cannot be navigated back to the client.
			// but as the initialization of list is failed. It will just
			// be printed as a error in the server console. This indicates
			// now the list is useless and every query is going to database
			// instead of fetching from the list.
			System.out.println("Exception " + se + " occured in the system."
					+ " Failed to initialize the list.");
		}
	}
	
	static 
	{
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:user_mysql:");
			initializeList(con);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
 	public int delete(int personId, Connection con) throws SQLException,PersonException {
		PreparedStatement ps = null;
		Person p = primaryList.get(personId);
		
		ps = con.prepareStatement("delete from  PERSON where personId = ?");
		ps.setInt(1, personId);
		int result = ps.executeUpdate();
		if (result <= 0) {
			throw new PersonException("No person found in the system.");
		}
		else
		{
			updateAllList(DELETE_OPERATION, p);
		}
		return result;

	}

 	public int updateFine(int personId, Person person, Connection con)
	throws SQLException, PersonException {
PreparedStatement ps = null;
ps = con.prepareStatement("update PERSON set FINE = ?  where personId = ?");
ps.setFloat(1, person.getFine());
ps.setInt(2, personId);

int result = ps.executeUpdate();
if (result <= 0) {
	throw new PersonException("Update of person record failed in the "
			+ "system. ");
}
else
{
	updateAllList(UPDATE_OPERATION, person);
}
return result;

}
 	
	public int update(int personId, Person person, Connection con)
			throws SQLException, PersonException {
		PreparedStatement ps = null;
		ps = con.prepareStatement("update PERSON set EMAILID = ?, FIRSTNAME = ? , LASTNAME = ? , ADDRESS = ? , CITY = ? , STATE = ? , ZIPCODE = ? , MEMBERTYPE = ? , CREDITCARDTYPE = ? , CREDITCARDNUM = ? , CREDITCARDCVV = ?, FINE = ?  where personId = ?");
		ps.setString(1, person.getEmailid());
		ps.setString(2, person.getFirstname());
		ps.setString(3, person.getLastname());
		ps.setString(4, person.getAddress());
		ps.setString(5, person.getCity());
		ps.setString(6, person.getState());
		ps.setString(7, person.getZipcode());
		ps.setString(8, person.getMembertype());
		ps.setString(9, person.getCreditcardtype());
		ps.setString(10, person.getCreditcardnum());
		ps.setInt(11, person.getCreditcardcvv());
		ps.setFloat(12, person.getFine());
		ps.setInt(13, personId);

		int result = ps.executeUpdate();
		if (result <= 0) {
			throw new PersonException("Update of person record failed in the "
					+ "system. ");
		}
		else
		{
			updateAllList(UPDATE_OPERATION, person);
		}
		return result;

	}

	public int insert(Person person, Connection con) throws PersonException,
			SQLException {

		PreparedStatement ps = null;
		ps = con.prepareStatement("insert into PERSON( EMAILID, FIRSTNAME, LASTNAME, ADDRESS, CITY, STATE, ZIPCODE, MEMBERTYPE, CREDITCARDTYPE, CREDITCARDNUM, CREDITCARDCVV) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
		ps.setString(1, person.getEmailid());
		ps.setString(2, person.getFirstname());
		ps.setString(3, person.getLastname());
		ps.setString(4, person.getAddress());
		ps.setString(5, person.getCity());
		ps.setString(6, person.getState());
		ps.setString(7, person.getZipcode());
		ps.setString(8, person.getMembertype());
		ps.setString(9, person.getCreditcardtype());
		ps.setString(10, person.getCreditcardnum());
		ps.setInt(11, person.getCreditcardcvv());

		int result = 0;
		if ((result = ps.executeUpdate()) <= 0) {
			throw new PersonException("Failed to insert new record in the "
					+ "system as the person id exists.");
		}
		else
		{
			con.commit();
			person = findByEmailid(person.getEmailid(), con);
			updateAllList(INSERT_OPERATION, person);
		}
		return result;

	}

	public Person findByPrimaryKey(int personid, Connection con)
			throws PersonException, SQLException {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Person result = null;

		if ((result = primaryList.get(new Integer(personid))) != null) {
			return result;
		}
		final String SQLSTATEMENT = "Select personId, emailId, firstName, "
				+ "lastName, address, city, state, zipCode, memberType, "
				+ "creditCardType, creditCardNum, creditCardCVV from "
				+ "person where personId = ?";
		stmt = con.prepareStatement(SQLSTATEMENT);
		stmt.setInt(1, personid);
		rs = stmt.executeQuery();

		result = fetchSingleResult(rs);

		if (result == null) {
			throw new PersonException(
					"No record found for the person using the primary key.");
		}
		else
		{
			primaryList.put(personid, result);
		}
		return result;

	}

	public Person findByEmailid(String emailid, Connection con)
			throws PersonException, SQLException {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Person result;
		
		result = emailIdList.get(emailid);
		
		if (result != null)
		{
			return result;
		}
		String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person where emailId = ? order by emailId";
		stmt = con.prepareStatement(SQL_STATEMENT);
		stmt.setString(1, emailid);
		rs = stmt.executeQuery();
		result = fetchSingleResult(rs);
		if (result == null) {
			throw new PersonException(
					"No persons found in the system with email Id" + emailid);
		}
		return result;
	}

	public Person[] findByFirstname(String firstname, Connection con)
			throws PersonException, SQLException {
		int size = nameList.get(firstname).size();
		Person result[] = nameList.get(firstname).toArray(new Person[size]);
		if (result == null || result.length <= 0) {
			throw new PersonException(
					"No Person found in the system with first name "
							+ firstname);
		}
//		PreparedStatement stmt = null;
//		ResultSet rs = null;
//		String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person where firstName = ? order by firstName";
//		stmt = con.prepareStatement(SQL_STATEMENT);
//		stmt.setString(1, firstname);
//		rs = stmt.executeQuery();
//		result = fetchMultiResults(rs);

		return result;
	}

	public Person[] findByLastname(String lastname, Connection con)
			throws SQLException, PersonException {
		int size = lNameList.get(lastname).size();
		Person result[] = lNameList.get(lastname).toArray(new Person[size]);
		if (result == null || result.length <= 0) {
			throw new PersonException(
					"No Person found in the system with last name " + lastname);
		}
//		PreparedStatement stmt = null;
//		ResultSet rs = null;
//		String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person where lastName = ? order by lastName";
//		stmt = con.prepareStatement(SQL_STATEMENT);
//		stmt.setString(1, lastname);
//		rs = stmt.executeQuery();
//		result = fetchMultiResults(rs);
		return result;
	}

	public Person[] findByCity(String city, Connection con)
			throws PersonException, SQLException {
		int size = cityList.get(city).size();
		Person person[] = cityList.get(city).toArray(new Person[size]);
		if (person == null || person.length <= 0)
		{
			throw new PersonException(
					"No Person found in the system with address : " + city);
		}
		
//		PreparedStatement stmt = null;
//		ResultSet rs = null;
//		String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person where city = ? order by city";
//		stmt = con.prepareStatement(SQL_STATEMENT);
//		stmt.setString(1, city);
//		rs = stmt.executeQuery();
//		person = fetchMultiResults(rs);
		return person;
	}

	public Person[] findByState(String state, Connection con)
			throws PersonException, SQLException {
		int size = stateList.get(state).size();
		Person[] person = stateList.get(state).toArray(new Person[size]);
		if (person == null || person.length <= 0)
		{
			throw new PersonException("No Person found in the system with state: "+ state);
		}
//		PreparedStatement stmt = null;
//		ResultSet rs = null;
//		String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person where state = ? order by state";
//		stmt = con.prepareStatement(SQL_STATEMENT);
//		stmt.setString(1, state);
//		rs = stmt.executeQuery();
//		person = fetchMultiResults(rs);

		return person;
	}

	public Person[] findByZipcode(String zipcode, Connection con)
			throws SQLException, PersonException {
		int size = zipCodeList.get(zipcode).size();
		Person person[] = zipCodeList.get(zipcode).toArray(new Person[size]);
		if (person == null || person.length <= 0)
		{
			throw new PersonException("No Person found in the system with state: "+ zipcode);
		}
//		PreparedStatement stmt = null;
//		ResultSet rs = null;
//		String SQL_STATEMENT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person where zipCode = ? order by zipCode";
//		stmt = con.prepareStatement(SQL_STATEMENT);
//		stmt.setString(1, zipcode);
//		rs = stmt.executeQuery();
//		person = fetchMultiResults(rs);

		return person;
	}

	public Person[] findAll(Connection con) throws PersonException,SQLException {
		int size = primaryList.size();
		Person person[] = primaryList.values().toArray(new Person[size]);
		if (person == null || person.length <= 0)
		{
			throw new PersonException("No Persons found in the system.");
		}
		return person;
	}
	
	public Person[] findExecutingUserSelect(String selectStatement,
			Object[] sqlParams, Connection con) throws PersonException, SQLException {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Person person[];
		final String SQL_STATEMENT = selectStatement;
		stmt = con.prepareStatement(SQL_STATEMENT);
		for (int i = 0; i < sqlParams.length; i++) {
			stmt.setObject(i + 1, sqlParams[i]);
		}
		rs = stmt.executeQuery();
		person = fetchMultiResults(rs);
		if (person == null || person.length <= 0)
		{
			throw new PersonException("No Persons found in the system using " +
									  "the select statement :"+selectStatement);
		}
		return person;
	}

	public Person[] findExecutingUserWhere(String whereClause,
			Object[] sqlParams, Connection con) throws PersonException, SQLException {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Person person[];
		String SQL_SELECT = "Select personId, emailId, firstName, lastName, address, city, state, zipCode, memberType, creditCardType, creditCardNum, creditCardCVV from person";
		final String SQL_STATEMENT = SQL_SELECT + " where " + whereClause;
		stmt = con.prepareStatement(SQL_STATEMENT);
		for (int i = 0; i < sqlParams.length; i++) {
			stmt.setObject(i + 1, sqlParams[i]);
		}
		rs = stmt.executeQuery();
		person = fetchMultiResults(rs);
		if (person == null || person.length <= 0)
		{
			throw new PersonException("No Persons found in the system using " +
									  "the select statement :"+whereClause);
		}
		return person;
	}

	protected Person fetchSingleResult(ResultSet rs) throws SQLException {
		if (rs.next()) {
			Person dto = new Person();
			populateVO(dto, rs);
			return dto;
		} else {
			return null;
		}
	}

	protected void populateVO(Person dto, ResultSet rs) throws SQLException {
		dto.setPersonid(rs.getInt("personId"));
		dto.setEmailid(rs.getString("emailId"));
		dto.setFirstname(rs.getString("firstName"));
		dto.setLastname(rs.getString("lastName"));
		dto.setAddress(rs.getString("address"));
		dto.setCity(rs.getString("city"));
		dto.setState(rs.getString("state"));
		dto.setZipcode(rs.getString("zipCode"));
		dto.setMembertype(rs.getString("memberType"));
		dto.setCreditcardtype(rs.getString("creditCardType"));
		dto.setCreditcardnum(rs.getString("creditCardNum"));
		dto.setCreditcardcvv(rs.getInt("creditCardCVV"));
	}
	
	
	

	protected Person[] fetchMultiResults(ResultSet rs) throws SQLException {
		Collection<Person> resultList = new ArrayList<Person>();
		while (rs.next()) {
			Person dto = new Person();
			populateVO(dto, rs);
			resultList.add(dto);
		}
		Person ret[] = new Person[resultList.size()];
		resultList.toArray(ret);
		return ret;
	}
	private void updateAllList(int operation, Person person)
	{
		if (nameList.get(person.getFirstname()) == null) {
			LinkedList<Person> lPerson = new LinkedList<Person>();
			nameList.put(person.getFirstname(), lPerson);
		}

		if (cityList.get(person.getCity()) == null) {
			LinkedList<Person> lPerson = new LinkedList<Person>();
			cityList.put(person.getCity(), lPerson);
		}
		
		if (stateList.get(person.getState()) == null) {
			LinkedList<Person> lPerson = new LinkedList<Person>();
			stateList.put(person.getState(), lPerson);
		}
		if (lNameList.get(person.getLastname()) == null) {
			LinkedList<Person> lPerson = new LinkedList<Person>();
			lNameList.put(person.getLastname(), lPerson);
		}
		
		if (zipCodeList.get(person.getZipcode()) == null) {
			LinkedList<Person> lPerson = new LinkedList<Person>();
			zipCodeList.put(person.getZipcode(), lPerson);
		}
		
		if (operation == DELETE_OPERATION || operation == UPDATE_OPERATION)
		{
			primaryList.remove(person.getPersonid());
			emailIdList.remove(person.getEmailid());
			
			cityList.get(person.getCity()).remove(person);
			stateList.get(person.getState()).remove(person);
			nameList.get(person.getFirstname()).remove(person);
			lNameList.get(person.getLastname()).remove(person);
			zipCodeList.get(person.getZipcode()).remove(person);
		}
		else if (operation == INSERT_OPERATION || operation == UPDATE_OPERATION)
		{
			primaryList.put(person.getPersonid(), person);
			emailIdList.put(person.getEmailid(), person);
			
			cityList.get(person.getCity()).add(person);
			stateList.get(person.getState()).add(person);
			nameList.get(person.getFirstname()).add(person);
			lNameList.get(person.getLastname()).add(person);
			zipCodeList.get(person.getZipcode()).add(person);
		}
	}
//	//Find fields which are updated after last read
//	private HashMap<String,Object> findUpdatedFields(Person newPersonObj, Person listPersonObj) {
//		
//		HashMap<String,Object> result = new HashMap<String,Object>(); 
//		
//		if(!newPersonObj.getCity().equals(listPersonObj.getCity())) {
//			result.put("city", newPersonObj.getCity());
//		}
//		if(!newPersonObj.getAddress().equals(listPersonObj.getAddress())) {
//			result.put("address", newPersonObj.getAddress());
//		}
//		if(!(newPersonObj.getCreditcardcvv() ==listPersonObj.getCreditcardcvv())) {
//			result.put("creditcardcvv", newPersonObj.getCreditcardcvv());
//		}
//		if(!newPersonObj.getCity().equals(listPersonObj.getCity())) {
//			result.put("city", newPersonObj.getCity());
//		}
//		if(!newPersonObj.getCity().equals(listPersonObj.getCity())) {
//			result.put("city", newPersonObj.getCity());
//		}
//		if(!newPersonObj.getCity().equals(listPersonObj.getCity())) {
//			result.put("city", newPersonObj.getCity());
//		}
//		if(!newPersonObj.getCity().equals(listPersonObj.getCity())) {
//			result.put("city", newPersonObj.getCity());
//		}
//		return result;
//	}

	public boolean isValid(Person person) throws PersonException {
		// TODO Auto-generated method stub
		String emailId = person.getEmailid();
		String firstName = person.getFirstname();
		String lastName = person.getLastname();
		String address = person.getAddress();
		String city = person.getCity();
		String state = person.getState();
		String zipCode = person.getZipcode();
		String memberType = person.getMembertype();
		String creditCardType = person.getCreditcardtype();
		String creditCardNum= person.getCreditcardnum();
		int creditCardCVV = person.getCreditcardcvv();
		
		
		if(emailId == null || emailId.equals("") || !isValidEmailAddress(emailId)) {
			throw new PersonException("Invalid Email Addess Format");
		}
		
		if(firstName == null || firstName.equals("")) {
			throw new PersonException("First Name cannot be empty");
		}
		
		if(lastName == null || lastName.equals("")) {
			throw new PersonException("Last Name cannot be empty");
		}
		
		if(address == null || address.equals("")) {
			throw new PersonException("Address cannot be empty");
		}
		
		if(city == null || city.equals("")) {
			throw new PersonException("City cannot be empty");
		}
		
		if(state == null || state.equals("")) {
			throw new PersonException("State cannot be empty");
		}
		else if (!abbreviations.contains(state.toUpperCase())){
			throw new PersonException("Invalid State");
		}
		
		if(zipCode == null || zipCode.equals("")) {
			throw new PersonException("Zipcode cannot be empty");
		}
		else if(!isValidZipCode(zipCode)) {
			throw new PersonException("Invalid ZipCode Format");
		}
		
		if(memberType == null || memberType.equals("") || !memberType.equals("PC") && !memberType.equals("SC") ) {
			throw new PersonException("Invalid Member Type");			
		}
		
		if(creditCardType == null || creditCardType.equals("")) {
			throw new PersonException("Please Select Credit Card Type");
		}
		
		
		if(creditCardNum == null || creditCardNum.equals("")){
			throw new PersonException("Please Enter Credit Card Number");
		}
		else {
			try {
				Double.parseDouble(creditCardNum);
				if(creditCardNum.length() > 16) {
					throw new PersonException("Credit Card Number cannot be greater than 16 digits");					
				}
				else if(creditCardNum.length() < 16){					
					throw new PersonException("Credit Card Number cannot be less than 16 digits");					
				}
			}
			catch(NumberFormatException e) {
				throw new PersonException("Invalid Credit Card Number");
			}
		}
		
		String cvv = "" + creditCardCVV;
		if(cvv.equals("")) {
			throw new PersonException("Invalid CVV code");
		}
		if(cvv.length() > 3) {
			throw new PersonException("cvv code"+cvv+" cannot be greater than 3 digits");					
		}
		else if(cvv.length() < 3){					
			throw new PersonException("cvv code"+cvv+" cannot be less than 3 digits");					
		}
			
		return true;
	}
	
	private boolean isValidZipCode(String zipCode) {
		// TODO Auto-generated method stub
		 
		 Pattern p = Pattern.compile("(([0-9]{5})|([0-9]{5}-[0-9]{4}))");

	      //Match the given string with the pattern
	    Matcher m = p.matcher(zipCode);

	      //check whether match is found 
	      return m.matches();
		
	}

	private boolean isValidEmailAddress(String aEmailAddress){
		
	      //Set the email pattern string
	      Pattern p = Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");

	      //Match the given string with the pattern
	      Matcher m = p.matcher(aEmailAddress);

	      //check whether match is found 
	      return m.matches();	      
	   
	}

	public boolean isExist(Person person,Connection con) throws PersonException, SQLException {
		// TODO Auto-generated method stub
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Person result;
		
		result = emailIdList.get(person.getEmailid());
		
		if (result != null)
		{
			throw new PersonException("Cache: Email Id already exists. Please provide another Email Id");
		}
		System.out.println(person.getEmailid());
		String SQL_STATEMENT = "Select count(personId) as personCount from " +
				"person where emailId = ? order by emailId";
		stmt = con.prepareStatement(SQL_STATEMENT);
		stmt.setString(1, person.getEmailid());
		System.out.println(person.getEmailid());
		rs = stmt.executeQuery();
		System.out.println(person.getEmailid());
		rs.next();
		if(rs.getInt("personCount") != 0) {
			throw new PersonException("Email Id already exists. Please provide another Email Id");
		}		
		
		return false;
	}
}
